﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Acando.Dynamics.CRM.Silverlight.Library.MetaSdk;
using System.ServiceModel;
using Acando.Dynamics.CRM.Silverlight.Library.CrmSdk;
using Acando.Dynamics.CRM.Silverlight.Library.Interfaces;
using System.Threading;

namespace Acando.Dynamics.CRM.Silverlight.Library.Helper
{
    public class WrapperServiceCall
    {
        private static OperationContextScope test0 = null;

        public static void ExecuteWrapper(MetadataServiceSoapClient metaservice, MetadataServiceRequest request, String organame)
        {
            using (new OperationContextScope((IContextChannel)metaservice.InnerChannel))
            {

                OperationContext.Current.OutgoingMessageHeaders.Add(CrmMessageHeaderHelper.GenerateAuthHeader(0, organame, null));
                metaservice.ExecuteAsync(request);
            }
        }


        public static void ExecuteWrapper(CrmServiceSoapClient crmservice, Request request, String organame)
        {
            # region doesn`t work
            //Thread localThread = new Thread(delegate()
            //{
            //    using (new OperationContextScope((IContextChannel)crmservice.InnerChannel))
            //    {
            //         Insert SOAP header
            //        OperationContext.Current.OutgoingMessageHeaders.Add(CrmMessageHeaderHelper.GenerateAuthHeader(0, "SAPSales", null));

            //    }
            //});

            //localThread.Start();
            //crmservice.ExecuteAsync(request);
            #endregion

            using (new OperationContextScope((IContextChannel)crmservice.InnerChannel))
            {
                // Insert SOAP header
                OperationContext.Current.OutgoingMessageHeaders.Add(CrmMessageHeaderHelper.GenerateAuthHeader(0, organame, null));
                crmservice.ExecuteAsync(request);
            }
        }



        public static void RetrieveWrapper(CrmServiceSoapClient crmservice, String entityTypeName, Guid id, ColumnSetBase columnset, String organame)
        {
            if (test0 == null)
                test0 = new OperationContextScope((IContextChannel)crmservice.InnerChannel);

            OperationContext.Current.OutgoingMessageHeaders.Add(CrmMessageHeaderHelper.GenerateAuthHeader(0, organame, null));
            crmservice.RetrieveAsync(entityTypeName, id, columnset);

            //using (new OperationContextScope((IContextChannel)crmservice.InnerChannel))
            //{
            //    // Insert SOAP header
            //    OperationContext.Current.OutgoingMessageHeaders.Add(CrmMessageHeaderHelper.GenerateAuthHeader(0, "SAPSales", null));
            //    crmservice.RetrieveAsync(entityTypeName, id, columnset);
            //}
        }

        public static void RetrieveMultipleWrapper(CrmServiceSoapClient crmservice, QueryBase query, String organame)
        {
            using (new OperationContextScope((IContextChannel)crmservice.InnerChannel))
            {
                OperationContext.Current.OutgoingMessageHeaders.Add(CrmMessageHeaderHelper.GenerateAuthHeader(0, organame, null));
                crmservice.RetrieveMultipleAsync(query);
            }
        }

        public static void CreateWrapper(CrmServiceSoapClient crmservice, BusinessEntity item, String organame)
        {
            using (new OperationContextScope((IContextChannel)crmservice.InnerChannel))
            {
                OperationContext.Current.OutgoingMessageHeaders.Add(CrmMessageHeaderHelper.GenerateAuthHeader(0, organame, null));
                crmservice.CreateAsync(item);
            }
        }

        public static void UpdateWrapper(CrmServiceSoapClient crmservice, BusinessEntity item, String organame)
        {
            using (new OperationContextScope((IContextChannel)crmservice.InnerChannel))
            {
                OperationContext.Current.OutgoingMessageHeaders.Add(CrmMessageHeaderHelper.GenerateAuthHeader(0, organame, null));
                crmservice.UpdateAsync(item);
            }
        }
    }
}
